package com.mxx.basic.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;

import com.mxx.basic.R;

/**
 * 作者：刘江飞
 * 时间：2020/8/8  14:05
 * 邮箱：642371742Z@qq.com
 * 描述：
 **/
public class ToastUtils {
    private ToastUtils() {
        /* cannot be instantiated */
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    public static void showToast(Context context, int resId) {
        showToast(context, context.getString(resId), Toast.LENGTH_SHORT);
    }

    public static void showToast(Context context, String message) {
        showToast(context, message, Toast.LENGTH_SHORT);
    }

    public static void showToast(Activity context, int resId) {
        showToast(context, context.getString(resId), Toast.LENGTH_SHORT);
    }

    public static void showToast(Activity context, int resId, int duration) {
        showToast(context, context.getString(resId), duration);
    }

    private static void showToast(Activity context, String message) {
        showToast(context, message, Toast.LENGTH_SHORT);
    }


    private static void showToast(Context context, int resId, int duration) {
        ///Toast.makeText(context, resId, duration).show();
        showToast(context, context.getString(resId), duration);
    }

    static Toast toast;
    @SuppressLint("StaticFieldLeak")
    static TextView contentView;

    private static void showToast(Context context, String message, int duration) {
        if (context == null) {
            return;
        }
        if (toast!=null){
            toast.cancel();
        }
        try {
            int dp30 = dp2px(context, 30);
            int dp20 = dp2px(context, 20);
            contentView = new TextView(context);
            contentView.setGravity(Gravity.CENTER);
            contentView.setBackgroundResource(R.drawable.bg_toast);
            contentView.setTextSize(12);
            contentView.setTextColor(context.getResources().getColor(android.R.color.white));
            contentView.setPadding(dp30, dp20, dp30, dp20);
            toast = new Toast(context);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.setView(contentView);
            toast.setDuration(duration);
            contentView.setText(message);
            toast.show();
        } catch (Exception e) {
            Toast.makeText(context, message, duration).show();
            e.printStackTrace();
        }
    }

    private static int dp2px(Context context, float value) {
        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, context.getResources().getDisplayMetrics()) + 0.5f);
    }

    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }
}
